'use strict';


/**
 * registry center
 */

var bodyParser = require('koa-better-body');
var koaBody = bodyParser({});

var Router = require('koa-router');
var __pathPrefix = '/registry';
var router = new Router({prefix: __pathPrefix});
module.exports = router.routes();

router.get('/', function*() {
  this.body = 'test works';
});

var proxyMgr = require('../proxy_mgr.js');

/**
 * post /registry
 {
    "host": 'http://localhost:9991',
    "urlmatch": '/^test/.*'
 }
 */
router.post('/', koaBody, function*() {
  var host = this.request.body.fields.host;
  var urlmatch = this.request.body.fields.urlmatch;
  console.log('register %s:%s', urlmatch, host);
  proxyMgr.add(urlmatch, host);
  this.body = 'ok';
});

